home *** CD-ROM | disk | FTP | other *** search
- A program is 'const correct' if it never mutates a constant object. This is achieved by using the keyword 'const'. Ex:
- if you pass a String to a function 'f()', and you wish to prohibit 'f()' from modifying the original String, you:
- can either pass by value: void f( String s ) { /*...*/ }
- or by constant reference: void f(const String& s ) { /*...*/ }
- or by constant pointer: void f(const String* sptr) { /*...*/ }
- but *not* by non-const ref: void f( String& s ) { /*...*/ }
- *nor* by non-const pointer: void f( String* sptr) { /*...*/ }
-
- Attempted changes to 's' within a fn that takes a 'const String&' are flagged as compile-time errors; neither run-time space nor speed is degraded.